package bitmap;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;

public class Mandelbrot
{

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception
	{
		double x=-0.10900;
		double y=0.901;
		//double side=5;
		int totalIteractions = 2000;
		for(Integer i=12;i<15;i++){
			double side =5./Math.pow(2.0,i.doubleValue());
			double mag = -Math.log10(side);
			int iter=totalIteractions;
			if(mag>1)
				iter = (int)(totalIteractions*mag);
			System.out.println(i+") "+iter);
			draw(x,y,side,iter,i);
		}
		
		
	}

	private static void draw(double x, double y, double side, int totalIteractions, int indexI) throws Exception
	{

//		double x=-.5;	
//		double y=0;
//		double side=3;
		
		double x1 = x-side/2;
		double x2 = x+side/2;
		double y1 = y-side/2;
		double y2 = y+side/2;

		int lenght = 2000;
		int width = 2000;
		double xstep = (x2 - x1) / lenght;
		double ystep = (y2 - y1) / width;

		double  gamma=255./(totalIteractions);

		String index =("00000"+indexI).substring( ("00000"+indexI).length()-3, ("00000"+indexI).length());
		
		int[][] rgbValues = new int[lenght][width];

		System.out.println("start");
		for (int i = 0; i < width; i++)
		{
			for (int j = 0; j < lenght; j++)
			{

				double r = x1 + (double) i * xstep;
				double c = y1 + (double) j * ystep;
				//System.out.println(real + "," + complex);
				int iter = 0;
				boolean div = false;
				double mod = 0;
				double real=r;
				double complex=c;
				while (iter <= totalIteractions && !div)
				{

					double realN = real * real - complex * complex + r;
					double complexN = 2 * real * complex + c;
					mod = realN * realN + complexN * complexN;
					if (mod > 2)
						div = true;
					real = realN;
					complex=complexN;
					iter++;
				}

				if (iter >= totalIteractions)
				{
					rgbValues[j][i] = 0;
				}
				else
				{
					//Color col = new Color(iter);
					//int color=BMPSave.getColor((int)(iter*3), 0, 0);
					//System.out.println(iter);
					rgbValues[j][i] =iter*9;
					//System.out.println(color);
					//rgbValues[j][i] =16777215;
				}
			}
		}

		//rgbValues[lenght/2][width/2] = 255 | 255 << 8 | 0 << 16;
		
		System.out.println("Stop");
		
		BMPText bt = new BMPText();
		rgbValues=bt.print("["+x+","+y+"] l="+side, rgbValues,BMPSave.WHITE,3);
		
		BMPSave bmp = new BMPSave();
		String path="c:/tmp/"+index+"mb"+"["+x+","+y+"] l="+side+".bmp";
		bmp.saveBMP(path, rgbValues);
		
		File input = new File(path);  

		//Read the file to a BufferedImage  
		BufferedImage imageB = ImageIO.read(input);

		//Create a file for the output  
		//File output = new File(path+".jpg");  
		
		Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
		ImageWriter writer = (ImageWriter)iter.next();
		ImageWriteParam iwp = writer.getDefaultWriteParam();
		iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
		iwp.setCompressionQuality(1.0f); 
		
		
		File file = new File(path+".jpg");
		FileImageOutputStream output = new FileImageOutputStream(file);
		writer.setOutput(output);
		IIOImage image = new IIOImage(imageB, null, null);
		writer.write(null, image, iwp);
		writer.dispose();
		

		//Write the image to the destination as a JPG  
		//ImageIO.write(image, "jpg", output);
		
	}
}
